package com.example.time;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.time.temporal.WeekFields;
import java.util.Arrays;
import java.util.List;

/**
 * @author hej
 * @date 2024/6/27 17:46
 */
public class DateWeek {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static void main(String[] args) {
        LocalDate initDate = LocalDate.of(2024, 12, 31);
        setWeek(initDate);


    }

    public static void setWeek(LocalDate initDate){
        LocalDate startDate = LocalDate.of(2024, 12, 31);
        LocalDate endDate = LocalDate.of(2025, 12, 31);
        //当前系统时间年 yyyy
        int thisYear = LocalDate.now().getYear();
        if(ObjectUtils.isNotEmpty(initDate)){
            startDate = initDate.plusYears(-1);
            //如果传入时间不为空，则说明要生成指定年的数据,当前年用指定年-1
            thisYear = initDate.plusYears(-1).getYear();
        }
        //System.out.println(ChronoUnit.DAYS.between(startDate, endDate));
        List<String> custs = Arrays.asList("IBM","INTEL","EMC","DELL","LANGCHAO","Default","APPLE");
        //todo 查询每个客户最大的周别，拿到结束日期
        //假设最大时间为startDate
        LocalDate lastDate = startDate;
        //取明年
        int nextYear = thisYear + 1;
        //明年最后一天
        LocalDate nextYearEnd = LocalDate.of(nextYear, 12, 31);
        //判断该年是52周还是53周（第一步：計算明年第53周有幾天）
        long tempCount = ChronoUnit.DAYS.between(lastDate, nextYearEnd) - (52 * 7);
        //判断该年是52周还是53周（第二步：判斷明年第53周是否>=7，是的話最後一週為53周，不是的話最後一週為52周，剩下的歸到下一年的第一週）
        if(tempCount <= 0){
            return;
            //continue;
        }

        //计算周时，默认周一作为一周的开始时间
        //int week = startDate.get(WeekFields.of(DayOfWeek.MONDAY,1).weekOfWeekBasedYear());
        LocalDate plusDay = lastDate;
        String firstDayStr,lastDayStr;
        int year,quarter,month,week;
        for(int i = 1; i <= 12; i++){
            //每周第一天
            plusDay = plusDay.plusDays(1);
            firstDayStr = plusDay.format(formatter);
            //每周最后一天
            plusDay = plusDay.plusDays(6);
            lastDayStr = plusDay.format(formatter);
            // 获取年
            year = plusDay.getYear();
            // 获取季度数字
            quarter = plusDay.get(IsoFields.QUARTER_OF_YEAR);
            //获取月
            month = plusDay.getMonthValue();
            //获取周
            week = i;
            System.out.println(year + "," + quarter + "," +month + "," +year + "," +week + "," +firstDayStr + "," +lastDayStr );
        }



    }

}
